import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class ImageLoader {

    private Display display = null;
    private Shell shell = null;
    private Composite imageCanvas = null;
    private Image img = null;
    private Color rectColor = null;

    private String[] imageURLs = new String[] {
            "http://upload.wikimedia.org/wikipedia/en/7/70/Example.png",
            "http://upload.wikimedia.org/wikipedia/commons/thumb/1/19/APEC_Police_Helicopter%2C_Opera_House%2C_2_Sept_2007.JPG/220px-APEC_Police_Helicopter%2C_Opera_House%2C_2_Sept_2007.JPG",
            "http://upload.wikimedia.org/wikipedia/commons/thumb/a/a2/Abroad_-_1882.djvu/page13-474px-Abroad_-_1882.djvu.jpg"
        };
    private int imgNum = 0;

    public ImageLoader() {
        display = new Display();
        shell = new Shell(display);
        shell.setLayout(new FillLayout());
        shell.setSize(700, 500);

        rectColor = new Color(display, new RGB(255, 0, 0));

        // load first image which should be displayed
        loadImage();

        imageCanvas = new Composite(shell, SWT.BORDER);
        // on each paint event (first view, window resize, redraw method, ...) make my code
        imageCanvas.addPaintListener(new PaintListener() {

            @Override
            public void paintControl(PaintEvent e) {
                // draw the image from (0,0) in it's own size
                e.gc.drawImage(img, 0, 0);
                // set foreground color (paint color)
                e.gc.setForeground(rectColor);
            }
        });

        Button btnNext = new Button(shell, SWT.PUSH);
        btnNext.setText("Next image");
        btnNext.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                // load next image
                loadImage();
                // repaint image canvas
                imageCanvas.redraw();
            }

        });

//      shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }

        rectColor.dispose();
    }

    /**
     * Loads next image from folder
     */
    private void loadImage() {
        try {
            img = new Image(display, getImageURL().openStream());
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Fakes loading next image from folder by cycling on image array
     * @return
     */
    private URL getImageURL() {
        URL tmpURL;
        try {
            tmpURL = new URL(imageURLs[imgNum]);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        }
        // in the end? let's turn back on start
        if(++imgNum >= imageURLs.length) imgNum = 0;
        return tmpURL;
    }

    public static void main(String[] args) {
        new ImageLoader();
    }
}